<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <span>自动化测试</span>
      </div>
      <el-button type="info" @click="loginOut">退出登录</el-button>
    </el-header>
    <el-container>
      <el-aside :width="!isCollapse ? '200px' : '70px'">
        <div class="toggle-button" @click="toggleCollapse(isCollapse)">
          <i class="el-icon-s-fold" v-if="isCollapse == false"></i>
          <i class="el-icon-s-unfold" v-else></i>
        </div>
        <el-menu
          :uniqueOpened="false"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse="isCollapse"
          :default-active="activePath"
          :collapse-transition="false"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.path" v-for="item in sysPermsList" :key="item.id">
            <template slot="title">
              <i :class="icon[item.id]"></i>
              <span>{{ item.title }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState(subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.title }}</span>
              </template>
            </el-menu-item>
          </el-submenu>

          <!-- <el-submenu index="1">
            <el-menu-item index="/interface">接口列表</el-menu-item>
            <el-menu-item index="/tasks">任务列表</el-menu-item>
            <el-menu-item index="/report">任务报告</el-menu-item>
          </el-submenu>
          <el-submenu index="2">
            <template slot="title">
              <i class="iconfont icon-xitongguanli1 dicon"></i>
              <span>系统管理</span>
            </template>
            <el-menu-item index="/users" path="/users">用户列表</el-menu-item>
            <el-menu-item index="/project">项目列表</el-menu-item>
            <el-menu-item index="/dict">系统字典</el-menu-item>
            <el-menu-item index="/permission">系统权限</el-menu-item>
            <el-menu-item index="/role">系统角色</el-menu-item>
          </el-submenu> -->
        </el-menu>
      </el-aside>
      <el-main>
        <router-view class="view"></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import Cookies from 'js-cookie'
export default {
  data() {
    return {
      sysPermsList: [],
      activePath: '',
      // 默认不折叠菜单
      isCollapse: false,
      icon: {
        '1': 'iconfont icon-xitongguanli1 dicon',
        '6': 'iconfont icon-renwu dicon'
      }
    }
  },
  created() {
    this.getUserPermission()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    getUserPermission() {
      this.sysPermsList = []
      this.$http
        .get('/sys/role/perms')
        .then(res => {
          const resData = res.data
          if (resData.code === 200) {
            this.sysPermsList = resData.data
            // console.log(this.sysPermsList)
          } else {
            return this.$message.error(resData.msg)
          }
        })
        .catch(err => {
          return this.$message.error(err)
        })
    },
    loginOut() {
      this.$router.push('/login')
      Cookies.remove('Authorization')
    },
    // 点击按钮 折叠/展开
    toggleCollapse(isCollapse) {
      // console.log(isCollapse)
      this.isCollapse = !isCollapse
    },
    // 当前活动路径
    saveNavState(path) {
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373441;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;

  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border: none;
  }
  .toggle-button {
    background-color: #4a5064;
    font-size: 24px;
    line-height: 34px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}

.el-main {
  background-color: #fff;
}

.iconfont {
  margin-right: 10px;
}

// .el-header {
//   background-color: #409eff;
//   position: relative;
//   width: 100%;
//   height: 60px;
//   display: flex;
//   align-items: center;

//   el-button {
//     display: flex;
//     justify-items: flex-end;
//   }
// }

// .el-side {
//   display: block;
//   position: absolute;
//   left: 0;
//   top: 60px;
//   bottom: 0;
// }

// .el-main {
//   background-color: #eee;
//   position: absolute;
//   left: 200px;
//   right: 0;
//   top: 60px;
//   bottom: 0;
//   overflow-y: scroll;
// }

// .el-container {
//   height: 100%;
// }

// .view {
//   height: 100%;
// }
</style>
